/*
 * @features: vmEvent 事件相关的
 * @description: 说明
 * @Date: 2021-10-02 14:11:11
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-10 17:38:03
 * @LastEditors: judu233
 */

import c3d from "../../../cc";
const { ccclass, property, executeInEditMode, disallowMultiple, menu } = c3d._decorator;
enum FILTER_MODE {
    "none",
    "==", //正常计算，比较 等于
    "!=", //正常计算，比较 不等于
    ">",  //正常计算，比较>
    ">=", //正常计算，比较>=
    "<",  //正常计算，比较<
    "<=", // 正常计算，比较>=
}
/**限制值边界范围的模式 */
enum CLAMP_MODE { MIN, MAX, MIN_MAX, }


@ccclass("EventHandleCall")
export class EventHandleCall {
    @property({ type: c3d.EventHandler, tooltip: "回调列表", displayName: "回调" })
    compEvents: c3d.EventHandler[] = [];

    @property({ type: [c3d.Node], displayName: `开启节点列表` })
    openActiveList: c3d.Node[] = [];

    @property({ type: [c3d.Node], displayName: `关闭节点列表` })
    closeActiveList: c3d.Node[] = [];

    /**
     * 派发ccEventHandler事件
     * @param values 参数
     */
    call(values: any[]) {
        this.close();
        this.open();
        this.emitEvent(values);
    }

    close() {
        this.closeActiveList.forEach(node => node && (node.active = false));
    }
    open() {
        this.openActiveList.forEach(node => node && (node.active = true));
    }
    emitEvent(...values: any[]) {
        if (this.compEvents.length == 0) {
            // console.warn(`EventHandleCall没有事件需要回调`, this);
            return;
        }
        /**默认的 EventHandler 的参数是在最后一个 翻转一下 */
        for (let event of this.compEvents) {
            if (event)
                event.emit([event, ...values])
        }
    }

}

@ccclass(`ValueChangeCall`)
export class ValueChangeCall {
    @property({ displayName: `一直激活`, tooltip: `开启后会一直处于激活状态，否则触发一次后会自动关闭该事件` })
    valueTriggerMore: boolean = false;

    @property({ displayName: `过滤模式`, tooltip: `过滤模式，会根据条件过滤掉时间的触发`, type: c3d.Enum(FILTER_MODE) })
    ValueFilterMode: FILTER_MODE = FILTER_MODE.none;

    @property({ displayName: `判断值`, visible(this: ValueChangeCall) { return this.ValueFilterMode !== FILTER_MODE.none } })
    valueCompare: string = ``;

    @property({ displayName: `回调`, type: [EventHandleCall] })
    valueChangeEvents: EventHandleCall[] = [];

    /**条件检查 */
    valueConditionCheck(a, b) {
        let cod = FILTER_MODE;
        switch (this.ValueFilterMode) {
            case cod.none: return true;
            case cod["=="]: return a == b;
            case cod["!="]: return a != b;
            case cod["<"]: return a < b;
            case cod[">"]: return a > b;
            case cod[">="]: return a >= b;
            case cod["<"]: return a < b;
            case cod["<="]: return a <= b;
            default: return false;
        }
    }
}


@ccclass(`VmEvent`)
export default class VmEvent {

    //#region 额外监听数值的回调
    @property({ displayName: `数值变动回调`, tooltip: `监听的数值模型发生变动` })
    isValueCall = false;

    @property({ displayName: `数值变动`, type: ValueChangeCall, visible() { return this.isValueCall } })
    valueChangeCall = new ValueChangeCall();

    @property({ displayName: `组件变动回调`, tooltip: `监听的组件数值发生变动` })
    isComCall = false;

    @property({ displayName: `组件变动`, type: ValueChangeCall, visible() { return this.isComCall } })
    comChangeCall = new ValueChangeCall();
}